#
#
#

VERSION         = @VERSION@
VERSIONHI       = @VERSIONHI@
VERSIONLO       = @VERSIONLO@
VERSIONP        = @VERSIONP@

SHELL           = /bin/sh
INSTALL         = @INSTALL@
CP              = @CP@

LIB_TYPE        = @LIB_TYPE@

ifeq ($(shell uname -s),Linux)
CP_U    = $(CP) -u
else
CP_U    = $(CP)
endif

top_builddir    = @top_builddir@
abs_top_builddir= @abs_top_builddir@
top_srcdir      = @top_srcdir@
VPATH           = @srcdir@
srcdir          = @srcdir@
INCDIR          = $(top_srcdir)/device/include
ifndef PORTINCDIR
PORTINCDIR      = $(INCDIR)/mcs51
endif

CC              = $(top_builddir)/bin/sdcc
# The predefined macros reflect what SDCC predefines (so we don't get any errors when generating Makefile.dep).
CPP             = $(top_builddir)/bin/sdcpp -D__STDC_ISO_10646__=201409L
SDAR            = $(abs_top_builddir)/bin/sdar

prefix          = @prefix@
exec_prefix     = @exec_prefix@
bindir          = @bindir@
libdir          = @libdir@
datadir         = @datadir@
datarootdir     = @datarootdir@
includedir      = @includedir@
mandir          = @mandir@
man1dir         = $(mandir)/man1
man2dir         = $(mandir)/man2
infodir         = @infodir@

lib_dir_suffix  = @lib_dir_suffix@

sdcc_libdir     = $(DESTDIR)$(datadir)/$(lib_dir_suffix)

CPPFLAGS        = -I$(INCDIR) -I$(PORTINCDIR)
CFLAGS          = $(MODELFLAGS) --nostdinc --std-c23

BUILDDIR        = build
# Default
PORT            = z80
ifndef PORTDIR
PORTDIR         = $(BUILDDIR)/$(PORT)
endif

OPT_DISABLE_DS390    = @OPT_DISABLE_DS390@
OPT_DISABLE_DS400    = @OPT_DISABLE_DS400@
OPT_DISABLE_HC08     = @OPT_DISABLE_HC08@
OPT_DISABLE_S08      = @OPT_DISABLE_S08@
OPT_DISABLE_MCS51    = @OPT_DISABLE_MCS51@
OPT_DISABLE_PIC14    = @OPT_DISABLE_PIC14@
OPT_DISABLE_PIC16    = @OPT_DISABLE_PIC16@
OPT_DISABLE_XA51     = @OPT_DISABLE_XA51@
OPT_DISABLE_Z80      = @OPT_DISABLE_Z80@
OPT_DISABLE_Z180     = @OPT_DISABLE_Z180@
OPT_DISABLE_EZ80     = @OPT_DISABLE_EZ80@
OPT_DISABLE_Z80N     = @OPT_DISABLE_Z80N@
OPT_DISABLE_R2K      = @OPT_DISABLE_R2K@
OPT_DISABLE_R2KA     = @OPT_DISABLE_R2KA@
OPT_DISABLE_R3KA     = @OPT_DISABLE_R3KA@
OPT_DISABLE_R4K      = @OPT_DISABLE_R4K@
OPT_DISABLE_R5K      = @OPT_DISABLE_R5K@
OPT_DISABLE_R6K      = @OPT_DISABLE_R6K@
OPT_DISABLE_TLCS90   = @OPT_DISABLE_TLCS90@
OPT_DISABLE_SM83     = @OPT_DISABLE_SM83@
OPT_DISABLE_R800     = @OPT_DISABLE_R800@
OPT_DISABLE_STM8     = @OPT_DISABLE_STM8@
OPT_DISABLE_PDK13    = @OPT_DISABLE_PDK13@
OPT_DISABLE_PDK14    = @OPT_DISABLE_PDK14@
OPT_DISABLE_PDK15    = @OPT_DISABLE_PDK15@
OPT_DISABLE_MOS6502  = @OPT_DISABLE_MOS6502@
OPT_DISABLE_MOS65C02 = @OPT_DISABLE_MOS65C02@
OPT_DISABLE_F8       = @OPT_DISABLE_F8@
OPT_DISABLE_F8L      = @OPT_DISABLE_F8L@

SOURCES_FLOAT = $(COMMON_FLOAT) \
  _fscmp.c \
  _fsget1arg.c \
  _fsget2args.c \
  _fsnormalize.c \
  _fsreturnval.c \
  _fsrshift.c \
  _fsswapargs.c \
  _logexpf.c

SOURCES_INT = $(COMMON_INT) \
  _divsint.c \
  _divuint.c \
  _modsint.c \
  _moduint.c \
  _mulint.c

SOURCES_LONG = $(COMMON_LONG) \
  _divulong.c \
  _mullong.c

SOURCES_LONGLONG = $(COMMON_LONGLONG) \
  _srulonglong.c \
  _srslonglong.c \
  _slulonglong.c \
  _slslonglong.c \
  _mullonglong.c \
  _divslonglong.c \
  _divulonglong.c \
  _modslonglong.c \
  _modulonglong.c

SOURCES_SDCC = $(COMMON_SDCC) \
  _autobaud.c \
  _bp.c \
  _decdptr.c \
  _gptrget.c \
  _gptrgetc.c \
  _gptrput.c \
  _ser.c \
  _setjmp.c \
  serial.c \
  __itoa.c \
  __ltoa.c \
  _spx.c \
  _startup.c \
  _strcmp.c \
  strlen.c \
  strnlen.c \
  __memcpy.c \
  memcpy.c \
  _memmove.c \
  _strcpy.c \
  _heap.c \
  div.c \
  ldiv.c \
  lldiv.c \
  imaxdiv.c \
  sprintf.c \
  vprintf.c \
  printf_fast.c \
  printf_fast_f.c \
  printf_tiny.c \
  printfl.c \
  bpx.c

SOURCES = $(SOURCES_FLOAT) $(SOURCES_INT) $(SOURCES_LONG) $(SOURCES_LONGLONG) $(SOURCES_SDCC)

OBJECTS_FLOAT = $(SOURCES_FLOAT:%.c=$(PORT)/%.rel)
OBJECTS_INT = $(SOURCES_INT:%.c=$(PORT)/%.rel)
OBJECTS_LONG = $(SOURCES_LONG:%.c=$(PORT)/%.rel)
OBJECTS_LONGLONG = $(SOURCES_LONGLONG:%.c=$(PORT)/%.rel)
OBJECTS_SDCC = $(SOURCES_SDCC:%.c=$(PORT)/%.rel)

OBJECTS = $(OBJECTS_FLOAT) $(OBJECTS_INT) $(OBJECTS_LONG) $(OBJECTS_LONGLONG) $(OBJECTS_SDCC)

XA51_FLOAT = $(COMMON_FLOAT)

XA51_INT = $(COMMON_INT) \
  _divsint.c \
  _divuint.c \
  _modsint.c \
  _moduint.c \
  _mulint.c

XA51_LONG = $(COMMON_LONG) \
  _divulong.c \
  _mullong.c

XA51_SDCC = $(COMMON_SDCC) \
  _memmove.c \
  strlen.c \
  strnlen.c \
  _heap.c \
  div.c \
  ldiv.c \
  lldiv.rel

XA51SOURCES = $(XA51_FLOAT) $(XA51_INT) $(XA51_LONG) $(XA51_SDCC)
XA51OBJECTS = $(XA51SOURCES:%.c=$(PORT)/%.rel)

LIB_FILES = $(PORTDIR)/libfloat.lib $(PORTDIR)/libint.lib $(PORTDIR)/liblong.lib $(PORTDIR)/liblonglong.lib $(PORTDIR)/libsdcc.lib

OEXT            = .rel

include $(srcdir)/incl.mk

ifeq ($(OPT_DISABLE_DS390), 0)
TARGETS        += model-ds390
endif

ifeq ($(OPT_DISABLE_DS400), 0)
TARGETS        += model-ds400
endif

ifeq ($(OPT_DISABLE_HC08), 0)
TARGETS        += model-hc08
endif

ifeq ($(OPT_DISABLE_S08), 0)
TARGETS        += model-s08
TARGETS        += model-s08-stack-auto
endif

ifeq ($(OPT_DISABLE_MOS6502), 0)
TARGETS        += model-mos6502
TARGETS        += model-mos6502-stack-auto
endif

ifeq ($(OPT_DISABLE_MOS65C02), 0)
TARGETS        += model-mos65c02
endif

ifeq ($(OPT_DISABLE_MCS51), 0)
TARGETS        += models small-mcs51-stack-auto large-mcs51-stack-auto
endif

ifeq ($(OPT_DISABLE_PIC14), 0)
TARGETS        += model-pic14
endif

ifeq ($(OPT_DISABLE_PIC16), 0)
TARGETS        += model-pic16
endif

ifeq ($(OPT_DISABLE_Z80), 0)
TARGETS        += model-z80
endif

ifeq ($(OPT_DISABLE_Z180), 0)
TARGETS        += model-z180
endif

ifeq ($(OPT_DISABLE_EZ80), 0)
TARGETS        += model-ez80
endif

ifeq ($(OPT_DISABLE_Z80N), 0)
TARGETS        += model-z80n
endif

ifeq ($(OPT_DISABLE_R2K), 0)
TARGETS        += model-r2k
endif

ifeq ($(OPT_DISABLE_R2KA), 0)
TARGETS        += model-r2ka
endif

ifeq ($(OPT_DISABLE_R3KA), 0)
TARGETS        += model-r3ka
endif

ifeq ($(OPT_DISABLE_R4K), 0)
TARGETS        += model-r4k
endif

ifeq ($(OPT_DISABLE_R5K), 0)
TARGETS        += model-r5k
endif

ifeq ($(OPT_DISABLE_R6K), 0)
TARGETS        += model-r6k
endif

ifeq ($(OPT_DISABLE_TLCS90), 0)
TARGETS        += model-tlcs90
endif

ifeq ($(OPT_DISABLE_SM83), 0)
TARGETS        += model-sm83
endif

ifeq ($(OPT_DISABLE_R800), 0)
TARGETS        += model-r800
endif

ifeq ($(OPT_DISABLE_STM8), 0)
TARGETS        += model-stm8
TARGETS        += model-stm8-large
endif

ifeq ($(OPT_DISABLE_PDK13), 0)
TARGETS        += model-pdk13
endif

ifeq ($(OPT_DISABLE_PDK14), 0)
TARGETS        += model-pdk14
endif

ifeq ($(OPT_DISABLE_PDK15), 0)
TARGETS        += model-pdk15
TARGETS        += model-pdk15-stack-auto
endif

ifeq ($(OPT_DISABLE_F8), 0)
TARGETS        += model-f8
endif

ifeq ($(OPT_DISABLE_F8L), 0)
TARGETS        += model-f8l
endif

# Compiling entire program or any subproject
# ------------------------------------------
all:
	$(MAKE) checkconf
ifneq ($(TARGETS), )
	$(MAKE) $(TARGETS)
endif

objects:
	$(MAKE) build-dir
	$(MAKE) $(OBJECTS)
	$(MAKE) port-specific-objects
	$(MAKE) $(LIB_FILES)
	$(MAKE) clean_intermediate

models:
	if grep '^mcs51$$' $(top_builddir)/ports.build > /dev/null; then \
	  for model in $(MODELS); do \
	    $(MAKE) MODELFLAGS="--model-$$model" PORT=$$model PORTINCDIR=$(INCDIR)/mcs51 objects || exit 1; \
	  done \
	fi

small-mcs51-stack-auto:
	if grep '^mcs51$$' $(top_builddir)/ports.build > /dev/null; then \
	  mkdir -p small-stack-auto; cp small/Makefile small-stack-auto; \
	  $(MAKE) MODELFLAGS="--model-small --stack-auto" PORT=small-stack-auto PORTDIR=$(BUILDDIR)/small-stack-auto PORTINCDIR=$(INCDIR)/mcs51 objects || exit 1; \
	fi

large-mcs51-stack-auto:
	if grep '^mcs51$$' $(top_builddir)/ports.build > /dev/null; then \
	  mkdir -p large-stack-auto; cp small/Makefile large-stack-auto; \
	  $(MAKE) MODELFLAGS="--model-large --stack-auto" PORT=large-stack-auto PORTDIR=$(BUILDDIR)/large-stack-auto PORTINCDIR=$(INCDIR)/mcs51 objects || exit 1; \
	fi

model-mcs51-stack-auto:
	if grep '^mcs51$$' $(top_builddir)/ports.build > /dev/null; then \
	  for model in $(MODELS); do \
	    mkdir -p $${model}-stack-auto; cp $${model}/Makefile $${model}-stack-auto; \
	    $(MAKE) MODELFLAGS="--model-$$model --stack-auto" PORT=$${model}-stack-auto PORTDIR=$(BUILDDIR)/$${model}-stack-auto PORTINCDIR=$(INCDIR)/mcs51 objects || exit 1; \
	  done \
	fi

model-mcs51-xstack-auto:
	if grep '^mcs51$$' $(top_builddir)/ports.build > /dev/null; then \
	  for model in $(MODELS); do \
	    mkdir -p $${model}-xstack-auto; cp $${model}/Makefile $${model}-xstack-auto; \
	    $(MAKE) MODELFLAGS="--model-$$model --stack-auto --xstack" PORT=$${model}-xstack-auto PORTDIR=$(BUILDDIR)/$${model}-xstack-auto PORTINCDIR=$(INCDIR)/mcs51 objects || exit 1; \
	  done \
	fi

model-ds390:
	if grep '^ds390$$' $(top_builddir)/ports.build > /dev/null; then \
	  $(MAKE) MODELFLAGS="-mds390" PORT=ds390 PORTINCDIR=$(INCDIR)/mcs51 objects || exit 1; \
	fi

model-ds400:
	if grep '^ds390$$' $(top_builddir)/ports.build > /dev/null; then \
	  $(MAKE) MODELFLAGS="-mds400" PORT=ds400 PORTINCDIR=$(INCDIR)/mcs51 objects || exit 1; \
	fi

model-xa51:
	if grep '^xa51$$' $(top_builddir)/ports.build > /dev/null; then \
	  $(MAKE) MODELFLAGS="-mxa51" PORT=xa51 objects-xa51 || exit 1; \
	fi

objects-xa51:
	$(MAKE) build-dir
	$(MAKE) $(XA51OBJECTS)
	$(MAKE) port-specific-objects

$(PORTDIR)/lib$(PORT).lib: $(XA51OBJECTS)
	rm -f $(PORTDIR)/lib$(PORT).lib
ifeq ($(LIB_TYPE), SDCCLIB)
	$(top_builddir)/bin/sdcclib -a $(PORTDIR)/lib$(PORT).lib $(XA51OBJECTS)
else
  ifeq ($(LIB_TYPE), AR)
	${SDAR} -rcSD $(PORTDIR)/lib$(PORT).lib $(XA51OBJECTS)
  else
    ifeq ($(LIB_TYPE), RANLIB)
	${SDAR} -rcD $(PORTDIR)/lib$(PORT).lib $(XA51OBJECTS)
    else
	rm -f $(PORTDIR)/lib$(PORT).lib
	for i in $(notdir $(basename $(XA51OBJECTS))); do echo $$i >> $(PORTDIR)/lib$(PORT).lib; done
	cp $(XA51OBJECTS) $(PORTDIR)
    endif
  endif
endif

model-z80:
	if grep '^z80$$' $(top_builddir)/ports.build > /dev/null; then \
	  $(MAKE) MODELFLAGS="-mz80" PORT=z80 objects-z80 OEXT=.o || exit 1; \
	fi

model-z180:
	if grep '^z80$$' $(top_builddir)/ports.build > /dev/null; then \
	  $(MAKE) MODELFLAGS="-mz180" PORT=z180 objects-z80 OEXT=.o || exit 1; \
	fi

model-ez80:
	if grep '^z80$$' $(top_builddir)/ports.build > /dev/null; then \
	  $(MAKE) MODELFLAGS="-mez80" PORT=ez80 objects-z80 OEXT=.o || exit 1; \
	fi

model-z80n:
	if grep '^z80$$' $(top_builddir)/ports.build > /dev/null; then \
	  $(MAKE) MODELFLAGS="-mz80n" PORT=z80n objects-z80 OEXT=.o || exit 1; \
	fi

model-r2k:
	if grep '^z80$$' $(top_builddir)/ports.build > /dev/null; then \
	  $(MAKE) MODELFLAGS="-mr2k" PORT=r2k objects-z80 OEXT=.o || exit 1; \
	fi

model-r2ka:
	if grep '^z80$$' $(top_builddir)/ports.build > /dev/null; then \
	  $(MAKE) MODELFLAGS="-mr2ka" PORT=r2ka objects-z80 OEXT=.o || exit 1; \
	fi

model-r3ka:
	if grep '^z80$$' $(top_builddir)/ports.build > /dev/null; then \
	  $(MAKE) MODELFLAGS="-mr3ka" PORT=r3ka objects-z80 OEXT=.o || exit 1; \
	fi

model-r4k:
	if grep '^z80$$' $(top_builddir)/ports.build > /dev/null; then \
	  $(MAKE) MODELFLAGS="-mr4k" PORT=r4k objects-z80 OEXT=.o || exit 1; \
	fi

model-r5k:
	if grep '^z80$$' $(top_builddir)/ports.build > /dev/null; then \
	  $(MAKE) MODELFLAGS="-mr5k" PORT=r5k objects-z80 OEXT=.o || exit 1; \
	fi

model-r6k:
	if grep '^z80$$' $(top_builddir)/ports.build > /dev/null; then \
	  $(MAKE) MODELFLAGS="-mr6k" PORT=r6k objects-z80 OEXT=.o || exit 1; \
	fi

model-tlcs90:
	if grep '^z80$$' $(top_builddir)/ports.build > /dev/null; then \
	  $(MAKE) MODELFLAGS="-mtlcs90" PORT=tlcs90 objects-z80 OEXT=.o || exit 1; \
	fi

model-sm83:
	if grep '^z80$$' $(top_builddir)/ports.build > /dev/null; then \
	  $(MAKE) MODELFLAGS="-msm83" PORT=sm83 objects-z80 OEXT=.o || exit 1; \
	fi

model-r800:
	if grep '^z80$$' $(top_builddir)/ports.build > /dev/null; then \
	  $(MAKE) MODELFLAGS="-mr800" PORT=r800 objects-z80 OEXT=.o || exit 1; \
	fi

objects-z80:
	$(MAKE) build-dir
	$(MAKE) port-specific-objects
	$(MAKE) clean_intermediate

model-hc08:
	if grep '^hc08$$' $(top_builddir)/ports.build > /dev/null; then \
	  $(MAKE) MODELFLAGS="-mhc08" PORT=hc08 objects-hc08 || exit 1; \
	fi

model-s08:
	if grep '^hc08$$' $(top_builddir)/ports.build > /dev/null; then \
	  $(MAKE) MODELFLAGS="-ms08" PORT=s08 objects-hc08 || exit 1; \
	fi

model-s08-stack-auto:
	if grep '^hc08$$' $(top_builddir)/ports.build > /dev/null; then \
	  $(MAKE) MODELFLAGS="-ms08 --stack-auto" PORT=s08-stack-auto objects-hc08 || exit 1; \
	fi

objects-hc08:
	$(MAKE) build-dir
	$(MAKE) port-specific-objects
	$(MAKE) clean_intermediate

model-mos6502:
	if grep '^mos6502$$' $(top_builddir)/ports.build > /dev/null; then \
	  $(MAKE) MODELFLAGS="-mmos6502" PORT=mos6502 objects-mos6502 || exit 1; \
	fi

model-mos6502-stack-auto:
	if grep '^mos6502$$' $(top_builddir)/ports.build > /dev/null; then \
	  $(MAKE) MODELFLAGS="-mmos6502 --stack-auto" PORT=mos6502-stack-auto objects-mos6502 || exit 1; \
	fi

model-mos65c02:
	if grep '^mos6502$$' $(top_builddir)/ports.build > /dev/null; then \
	  $(MAKE) MODELFLAGS="-mmos65c02" PORT=mos65c02 objects-mos6502 || exit 1; \
	fi

objects-mos6502:
	$(MAKE) build-dir
	$(MAKE) port-specific-objects
	$(MAKE) clean_intermediate

model-stm8:
	if grep '^stm8$$' $(top_builddir)/ports.build > /dev/null; then \
	  $(MAKE) MODELFLAGS="-mstm8" PORT=stm8 objects-stm8 || exit 1; \
	fi

objects-stm8:
	$(MAKE) build-dir
	$(MAKE) port-specific-objects
	$(MAKE) clean_intermediate

model-stm8-large:
	if grep '^stm8$$' $(top_builddir)/ports.build > /dev/null; then \
	  $(MAKE) MODELFLAGS="-mstm8 --model-large" PORT=stm8-large objects-stm8 || exit 1; \
	fi

model-pdk13:
	if grep '^pdk$$' $(top_builddir)/ports.build > /dev/null; then \
	  $(MAKE) MODELFLAGS="-mpdk13" PORT=pdk13 objects-pdk13 || exit 1; \
	fi

objects-pdk13:
	$(MAKE) build-dir
	$(MAKE) port-specific-objects
	$(MAKE) clean_intermediate

model-pdk14:
	if grep '^pdk$$' $(top_builddir)/ports.build > /dev/null; then \
	  $(MAKE) MODELFLAGS="-mpdk14" PORT=pdk14 objects-pdk14 || exit 1; \
	fi

objects-pdk14:
	$(MAKE) build-dir
	$(MAKE) port-specific-objects
	$(MAKE) clean_intermediate

model-pdk15:
	if grep '^pdk$$' $(top_builddir)/ports.build > /dev/null; then \
	  $(MAKE) MODELFLAGS="-mpdk15" PORT=pdk15 objects-pdk15 || exit 1; \
	fi

objects-pdk15:
	$(MAKE) build-dir
	$(MAKE) port-specific-objects
	$(MAKE) clean_intermediate

model-pdk15-stack-auto:
	if grep '^pdk$$' $(top_builddir)/ports.build > /dev/null; then \
	  $(MAKE) MODELFLAGS="-mpdk15 --stack-auto" PORT=pdk15-stack-auto objects-pdk15 || exit 1; \
	fi

model-f8:
	if grep '^f8$$' $(top_builddir)/ports.build > /dev/null; then \
	  $(MAKE) MODELFLAGS="-mf8" PORT=f8 objects-f8; \
	fi

objects-f8:
	$(MAKE) build-dir
	$(MAKE) port-specific-objects
	$(MAKE) clean_intermediate

model-f8l:
	if grep '^f8$$' $(top_builddir)/ports.build > /dev/null; then \
	  $(MAKE) MODELFLAGS="-mf8l" PORT=f8l objects-f8l; \
	fi

objects-f8l:
	$(MAKE) build-dir
	$(MAKE) port-specific-objects
	$(MAKE) clean_intermediate

model-pic16:
	if grep '^pic16$$' $(top_builddir)/ports.build > /dev/null; then \
	  $(MAKE) PORT=pic16 objects-pic16; \
	fi

objects-pic16:
	$(MAKE) build-dir
	$(MAKE) port-specific-objects-pic16
	$(MAKE) clean_intermediate-pic16

model-pic14:
	if grep '^pic14$$' $(top_builddir)/ports.build > /dev/null; then \
	  $(MAKE) PORT=pic14 objects-pic14; \
	fi

objects-pic14:
	$(MAKE) build-dir
	$(MAKE) port-specific-objects-pic14
	$(MAKE) clean_intermediate-pic14

build-dir:
	$(MAKE) $(PORTDIR)
	$(MAKE) $(PORT)

$(PORTDIR):
	mkdir -p $(PORTDIR)

$(PORT):
	mkdir -p $(PORT)

port-specific-objects:
	if [ -f $(PORT)/Makefile ]; then \
	  $(MAKE) -C $(PORT) PORT=$(PORT) || exit 1; \
	fi || :

port-specific-objects-pic16:
	# BUG: ignore errors
	if [ -f $(PORT)/Makefile ]; then \
	  $(MAKE) -C $(PORT) || exit 0; \
	  for i in $(PORT)/*/lib*.a; do cp -f $$i $(PORTDIR)/`basename $$i .a`.lib; done; \
	  cp -f $(PORT)/*/crt0*.o $(PORTDIR); \
	fi || :

port-specific-objects-pic14:
	if [ -f $(PORT)/Makefile ]; then \
	  $(MAKE) -C $(PORT) || exit 1; \
          find $(PORT) -name 'lib*.a' | while read i; do \
            cp -f "$$i" $(PORTDIR)/`basename "$$i" .a`.lib; \
          done; \
	fi || :

ifeq ($(LIB_TYPE), SDCCLIB)

$(PORTDIR)/libfloat.lib: $(OBJECTS_FLOAT)
	$(top_builddir)/bin/sdcclib -a $(PORTDIR)/libfloat.lib $(OBJECTS_FLOAT)

$(PORTDIR)/libint.lib: $(OBJECTS_INT)
	$(top_builddir)/bin/sdcclib -a $(PORTDIR)/libint.lib $(OBJECTS_INT)

$(PORTDIR)/liblong.lib: $(OBJECTS_LONG)
	$(top_builddir)/bin/sdcclib -a $(PORTDIR)/liblong.lib $(OBJECTS_LONG)

$(PORTDIR)/liblonglong.lib: $(OBJECTS_LONGLONG)
	$(top_builddir)/bin/sdcclib -a $(PORTDIR)/liblonglong.lib $(OBJECTS_LONGLONG)

$(PORTDIR)/libsdcc.lib: $(OBJECTS_SDCC)
	$(top_builddir)/bin/sdcclib -a $(PORTDIR)/libsdcc.lib $(OBJECTS_SDCC)

else
  ifeq ($(LIB_TYPE), AR)

$(PORTDIR)/libfloat.lib: $(OBJECTS_FLOAT)
	${SDAR} -rcSD $(PORTDIR)/libfloat.lib $(OBJECTS_FLOAT)

$(PORTDIR)/libint.lib: $(OBJECTS_INT)
	${SDAR} -rcSD $(PORTDIR)/libint.lib $(OBJECTS_INT)

$(PORTDIR)/liblong.lib: $(OBJECTS_LONG)
	${SDAR} -rcSD $(PORTDIR)/liblong.lib $(OBJECTS_LONG)

$(PORTDIR)/liblonglong.lib: $(OBJECTS_LONGLONG)
	${SDAR} -rcSD $(PORTDIR)/liblonglong.lib $(OBJECTS_LONGLONG)

$(PORTDIR)/libsdcc.lib: $(OBJECTS_SDCC)
	${SDAR} -rcSD $(PORTDIR)/libsdcc.lib $(OBJECTS_SDCC)

  else
    ifeq ($(LIB_TYPE), RANLIB)

$(PORTDIR)/libfloat.lib: $(OBJECTS_FLOAT)
	${SDAR} -rcD $(PORTDIR)/libfloat.lib $(OBJECTS_FLOAT)

$(PORTDIR)/libint.lib: $(OBJECTS_INT)
	${SDAR} -rcD $(PORTDIR)/libint.lib $(OBJECTS_INT)

$(PORTDIR)/liblong.lib: $(OBJECTS_LONG)
	${SDAR} -rcD $(PORTDIR)/liblong.lib $(OBJECTS_LONG)

$(PORTDIR)/liblonglong.lib: $(OBJECTS_LONGLONG)
	${SDAR} -rcD $(PORTDIR)/liblonglong.lib $(OBJECTS_LONGLONG)

$(PORTDIR)/libsdcc.lib: $(OBJECTS_SDCC)
	${SDAR} -rcD $(PORTDIR)/libsdcc.lib $(OBJECTS_SDCC)

    else

$(PORTDIR)/libfloat.lib: $(OBJECTS_FLOAT)
	rm -f $(PORTDIR)/libfloat.lib
	for i in $(notdir $(basename $(OBJECTS_FLOAT))); do echo $$i >> $(PORTDIR)/libfloat.lib; done
	cp $(OBJECTS_FLOAT) $(PORTDIR)

$(PORTDIR)/libint.lib: $(OBJECTS_INT)
	rm -f $(PORTDIR)/libint.lib
	for i in $(notdir $(basename $(OBJECTS_INT))); do echo $$i >> $(PORTDIR)/libint.lib; done
	cp $(OBJECTS_INT) $(PORTDIR)

$(PORTDIR)/liblong.lib: $(OBJECTS_LONG)
	rm -f $(PORTDIR)/liblong.lib
	for i in $(notdir $(basename $(OBJECTS_LONG))); do echo $$i >> $(PORTDIR)/liblong.lib; done
	cp $(OBJECTS_LONG) $(PORTDIR)

$(PORTDIR)/liblonglong.lib: $(OBJECTS_LONGLONG)
	rm -f $(PORTDIR)/liblonglong.lib
	for i in $(notdir $(basename $(OBJECTS_LONGLONG))); do echo $$i >> $(PORTDIR)/liblonglong.lib; done
	cp $(OBJECTS_LONGLONG) $(PORTDIR)

$(PORTDIR)/libsdcc.lib: $(OBJECTS_SDCC)
	rm -f $(PORTDIR)/libsdcc.lib
	for i in $(notdir $(basename $(OBJECTS_SDCC))); do echo $$i >> $(PORTDIR)/libsdcc.lib; done
	cp $(OBJECTS_SDCC) $(PORTDIR)

    endif
  endif
endif

clean_intermediate:
	rm -f $(PORTDIR)/*.lst $(PORTDIR)/*.sym

clean_intermediate-pic16:
	$(MAKE) -C $(PORT) clean-intermediate

clean_intermediate-pic14:
	$(MAKE) -C $(PORT) clean-intermediate

# Compiling and installing everything and runing test
# ---------------------------------------------------
install: all installdirs
	[ ! -d "$(BUILDDIR)" ] || $(CP_U) -r $(BUILDDIR)/* $(sdcc_libdir)
	$(CP_U) -r $(MODELS) $(srcdir)/mcs51 $(srcdir)/ds390 $(srcdir)/ds400 \
	                     $(srcdir)/z80 $(srcdir)/z180 $(srcdir)/ez80 $(srcdir)/z80n $(srcdir)/r2k $(srcdir)/r2ka $(srcdir)/r3ka $(srcdir)/r4k $(srcdir)/r5k $(srcdir)/r6k $(srcdir)/tlcs90 $(srcdir)/sm83 $(srcdir)/r800 \
	                     $(srcdir)/hc08 $(srcdir)/s08 \
	                     $(srcdir)/mos6502 $(srcdir)/mos65c02 \
	                     $(srcdir)/stm8 \
	                     $(srcdir)/pdk13 $(srcdir)/pdk14 $(srcdir)/pdk15 \
	                     $(srcdir)/f8 $(srcdir)/f8l \
	                     $(srcdir)/pic14 $(srcdir)/pic16 $(srcdir)/*.c $(sdcc_libdir)/src
	$(CP_U) -r $(MODELS) mcs51 ds390 ds400 z80 z180 ez80 z80n r2k r2ka r3ka r4k r5k r6k tlcs90 sm83 r800 hc08 s08 stm8 pdk13 pdk14 pdk15 f8 f8l $(sdcc_libdir)/src

	for src in $(MODELS) ds390 ds400 z80 z180 ez80 z80n r2k r2ka r3ka r4k r5k r6k tlcs90 sm83 r800 hc08 s08 mos6502 mos65c02 stm8 pdk13 pdk14 pdk15 f8 f8l pic14 pic16; do \
	  find $(sdcc_libdir)/src/$$src -depth \
	    \( -name '*.rel' -o -name '*.dump*' -o -name '*.sym' -o -name '*.o' -o -name '*.lib' -o \
	    -name '*.lst' -o -name '*.asm' -o -name '.svn' -o -name 'autom4te.cache' -o \
	    -name 'build' -o -name 'bin' -o -name 'Makefile*' -o -name 'config*' -o \
	    -name 'depcomp' -o -name 'missing' -o -name 'install-sh' -o \
	    -name 'bootstrap.sh' \) \
	    -exec rm -rf {} + ; \
	done
	find $(sdcc_libdir)/src/mcs51 -depth \
	  \( -name '*.rel' -o -name '*.dump*' -o -name '*.sym' -o -name '*.o' -o -name '*.lib' -o \
	  -name '*.lst' -o -name '.svn' -o -name 'build' -o -name 'bin' -o \
	  -name 'Makefile*' \) \
	  -exec rm -rf {} +

# Deleting all the installed files
# --------------------------------
uninstall:
	rm -rf $(sdcc_libdir)

# Performing self-test
# --------------------
check:


# Performing installation test
# ----------------------------
installcheck:


# Creating installation directories
# ---------------------------------
installdirs:
	for model in $(MODELS); do \
	  mkdir -p $(sdcc_libdir)/$$model; \
	done
	for libdir in small-stack-auto ds390 ds400 hc08 s08 pic16 pic14; do \
	  mkdir -p $(sdcc_libdir)/$$libdir; \
	done
	mkdir -p $(sdcc_libdir)/src

# Creating dependencies
# ---------------------
dep: Makefile.dep

Makefile.dep: $(SOURCES) $(INCDIR)/*.h $(PORTINCDIR)/*.h
	rm -f Makefile.dep
	for i in $(filter %.c,$^); do \
	  $(CPP) -std=c23 -M $(CPPFLAGS) $$i >$${i}.dep; \
	  cat $${i}.dep >>Makefile.dep; \
	  rm $${i}.dep; \
	done

ifeq "$(findstring $(MAKECMDGOALS),uninstall check installcheck installdirs checkconf \
                                   clean distclean mostlyclean realclean)" ""
  -include Makefile.dep
endif
include $(srcdir)/clean.mk

# My rules
# --------

.SUFFIXES: .rel .o

$(PORT)/%$(OEXT): %.c
	$(CC) $(CPPFLAGS) $(CFLAGS) -c $< -o $@

# Remaking configuration
# ----------------------
checkconf:
	@if [ -f $(top_builddir)/devel ]; then\
	  $(MAKE) -f $(srcdir)/conf.mk srcdir="$(srcdir)" top_builddir="$(top_builddir)" \
	  freshconf;\
	fi

# End of main_in.mk/main.mk

Makefile: $(srcdir)/Makefile.in
	cd $(top_builddir); ./config.status device/lib/Makefile
